package com.company.cloud.club.modules.controller;

import cn.hutool.core.codec.Base64;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.club.modules.core.dto.Result;
import com.company.cloud.club.modules.core.util.DateUtil;
import com.company.cloud.club.modules.core.util.ValidateUtil;
import com.company.cloud.club.modules.db.domain.ClubFeedback;
import com.company.cloud.club.modules.db.domain.UserInfo;
import com.company.cloud.club.modules.dto.IdQuery;
import com.company.cloud.club.modules.dto.LayTableResult;
import com.company.cloud.club.modules.dto.feedback.FeedbackBo;
import com.company.cloud.club.modules.dto.feedback.ForbiddenWordBo;
import com.company.cloud.club.modules.service.ClubFeedbackService;
import com.company.cloud.club.modules.service.UserInfoService;
import com.company.cloud.club.modules.utils.LoginContextUtils;
import com.company.cloud.club.modules.vo.FeedbackVo;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.auth.annotation.StaffLogc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * *****************************************************************************
 * <p>
 * 功能名           ：com.company.cloud.club.modules.controller
 * 系统名           ：
 * <p>
 * *****************************************************************************
 * Modification History
 * <p>
 * Date        Name                    Reason for Change
 * ----------  ----------------------  -----------------------------------------
 * 2021/9/4     zhangxu                 Created
 */
@Slf4j
@RestController
@Api(tags = "FeedbackController")
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    private ClubFeedbackService clubFeedbackService;

    @Autowired
    private UserInfoService userInfoService;
    
    @ApiOperation("举报列表")
    @GetMapping(value = "/admin/list")
    public LayTableResult getFeedbackList(@Valid @ModelAttribute ClubFeedback params) {
        List<FeedbackVo> list = new ArrayList<>();
        params.setDeleted(0);
        Integer currentPage = params.getCurrentPage();
        Integer pageSize = params.getPageSize();
        IPage<ClubFeedback> page = clubFeedbackService.page(new Page<>(currentPage, pageSize), new QueryWrapper<>(params));
        int count = clubFeedbackService.count(new QueryWrapper<>(params));
        for (ClubFeedback feedback : page.getRecords()) {
            FeedbackVo vo = new FeedbackVo();
            BeanUtils.copyProperties(feedback, vo);
            UserInfo userInfo = userInfoService.getById(feedback.getFbUserId());
            if (ValidateUtil.isValid(userInfo)) {
                vo.setFbUserId(userInfo.getId());
                vo.setFbUserName(userInfo.getNickName());
            }
            vo.setCtime(DateUtil.timeToStr(feedback.getTimeCreated(), DateUtil.DATE_MINI_PATTERN));
            list.add(vo);
        }
        return new LayTableResult(Result.SUCCESS_CODE, "获取成功", list, count);
    }

    @ApiOperation("举报信息")
    @PostMapping(value = "/create")
    public Result create(@RequestBody FeedbackBo bo) {
        Integer currentUserId = LoginContextUtils.getUserId();

        ClubFeedback feedback = new ClubFeedback(0);
        feedback.setFbUserId(currentUserId);
        feedback.setFbContent(bo.getFbContent());
        feedback.setFbContentId(bo.getFbContentId());
        feedback.setReason(bo.getReason());
        feedback.setType(bo.getType());
        feedback.setStatus((byte) 0);
        boolean flag = clubFeedbackService.save(feedback);
        if (!flag) {
            throw new RuntimeException("举报失败");
        }
        return Result.ok("举报成功", flag);
    }

    @PostMapping(value = "/deal")
    public Result deal(@Valid @RequestBody IdQuery bo) {
        Integer currentUserId = LoginContextUtils.getUserId();

        boolean flag = clubFeedbackService.deal(bo.getId());
        if (!flag) {
            throw new RuntimeException("处理失败");
        }
        return Result.ok("处理成功", flag);
    }

    @PostMapping(value = "/forbidden")
    @StaffLogc(title = "禁言操作", action = "禁言操作", actionAdo = "禁言列表")
    public Result forbiddenWord(@Valid @RequestBody ForbiddenWordBo bo) {
        LoginContextUtils.getUserId();
        String type = bo.getType();
        Integer fbUserId = bo.getFbUserId();
        UserInfo userInfo = userInfoService.getById(fbUserId);
        if ("禁言".equals(type)) {
            userInfo.setForbiddenDt(LocalDateTime.now().plusDays(6));
            userInfoService.updateById(userInfo);
            return Result.ok("禁言成功", "SUCCESS");
        } else if ("解禁".equals(type)) {
            userInfo.setForbiddenDt(null);
            userInfoService.updateById(userInfo);
            return Result.ok("禁言成功", "SUCCESS");
        }
        throw new RuntimeException("操作失败");
    }

}